/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.action;

import com.myapp.struts.form.Estimate5bForm;
import com.myapp.struts.form.EstimateForm;
import com.myapp.util.DBConfig;
import com.myapp.util.DateTimeUtils;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author toint
 */
public class AddEstimateAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest res, HttpServletResponse req) throws Exception {

        req.setContentType("charset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        EstimateForm frm = (EstimateForm) form;
        Map<String, String> mapUnit = frm.getMapUnit();
        Map<String, String> mapQuantity = frm.getMapQuantity();
        Map<String, String> mapSize = frm.getMapSize();
        Map<String, String> mapPersonHelp = frm.getMapPersonHelp();
        Map<String, String> mapTotal = frm.getMapTotal();
        Map<String, String> mapHuyDong = frm.getMapHuyDong();
        Map<String, String> mapYeuCau = frm.getMapYeuCau();
        Map<String, String> mapKichCo = frm.getMapKichCo();
        Map<String, String> mapMucDo = frm.getMapMucDo();
        Integer[] arrWantId = frm.getArrWantId();

        for (int i = 0; i < arrWantId.length; i++) {
            Integer wantId = arrWantId[i];
            Integer quantity = 0;
            Integer personHelp = 0;
            Integer helpTotal  = 0;//thon
            Integer helpSuport = 0;//noi luc
            Integer helpRequie = 0;
            String kichco = "";
            Integer levelPoint = 0;
            String unit = "";
            String size = "";
            Set enUnit = mapUnit.entrySet();
            Iterator iterUnit = enUnit.iterator();
            while (iterUnit.hasNext()) {
                Map.Entry entry = (Map.Entry) iterUnit.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    unit = str[0];
                }
            }
            Set enQuantity = mapQuantity.entrySet();
            Iterator iterQuantity = enQuantity.iterator();
            while (iterQuantity.hasNext()) {
                Map.Entry entry = (Map.Entry) iterQuantity.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String strQuantity = str[0];
                    if (strQuantity != null && !"".equals(strQuantity)) {
                        quantity = Integer.parseInt(strQuantity);
                    }
                }
            }
            Set enSize = mapSize.entrySet();
            Iterator iterSize = enSize.iterator();
            while (iterSize.hasNext()) {
                Map.Entry entry = (Map.Entry) iterSize.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        size = str1;
                    }
                }
            }
            Set enPersonHelp = mapPersonHelp.entrySet();
            Iterator iterPerHelp = enPersonHelp.iterator();
            while (iterPerHelp.hasNext()) {
                Map.Entry entry = (Map.Entry) iterPerHelp.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        personHelp = Integer.parseInt(str1);
                    }
                }
            }
            Set enTotal = mapTotal.entrySet();
            Iterator iterTotal = enTotal.iterator();
            while (iterTotal.hasNext()) {
                Map.Entry entry = (Map.Entry) iterTotal.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        helpTotal = Integer.parseInt(str1);
                    }
                }
            }
            Set enHuyDong = mapHuyDong.entrySet();
            Iterator iterSuport = enHuyDong.iterator();
            while (iterSuport.hasNext()) {
                Map.Entry entry = (Map.Entry) iterSuport.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        helpSuport = Integer.parseInt(str1);
                    }
                }
            }
            Set enRequie = mapYeuCau.entrySet();
            Iterator iterRequie = enRequie.iterator();
            while (iterRequie.hasNext()) {
                Map.Entry entry = (Map.Entry) iterRequie.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        helpRequie = Integer.parseInt(str1);
                    }
                }
            }
            Set enKt = mapKichCo.entrySet();
            Iterator iterKt = enKt.iterator();
            while (iterKt.hasNext()) {
                Map.Entry entry = (Map.Entry) iterKt.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        kichco = str1;
                    }
                }
            }
            Set enLevel = mapMucDo.entrySet();
            Iterator interLevel = enLevel.iterator();
            while (iterKt.hasNext()) {
                Map.Entry entry = (Map.Entry) interLevel.next();
                String key = (String) entry.getKey();
                String[] str = (String[]) entry.getValue();
                if (wantId == Integer.parseInt(key)) {
                    String str1 = str[0];
                    if (str1 != null && !"".equals(str1)) {
                        levelPoint = Integer.parseInt(str1);
                    }
                }
            }

            String sql = "insert into estimate_5a values (";
            sql += wantId + ", ";
            sql += "N'" + unit + "', ";
            sql += quantity + ", ";
            //sql += size + ", ";
            sql += personHelp + ", ";
            sql += helpTotal + ", ";
            sql += helpSuport + ", ";
            sql += helpRequie + ", ";
            sql += "'"+ kichco +"', ";
            sql += levelPoint +", ";
            sql += 2 + ", ";
            sql += "'" + new java.sql.Date(System.currentTimeMillis()) + "')";
            Connection conn = null;
            try {
                conn = DBConfig.getConnection();
                CallableStatement stm = conn.prepareCall(sql);
                stm.execute();
            } catch (Exception ex) {
                conn.close();
                System.err.println(ex);
                mapping.findForward("failed");
                break;
            }
        }

        return mapping.findForward("success");
    }

}
